package com.dyz.leetcode._12_linkedList;

import com.dyz.leetcode.ListNode;

public class _876_middleNode {
    public ListNode middleNode(ListNode head) {
        if(head==null){return null;}
        //计算长度
        int length=0;
        ListNode curr = head;
        while (curr!=null){
            length++;
            curr = curr.next;
        }

        for(int i =0; i<length/2; i++){
            head = head.next;
        }
        return head;

    }
    //快慢指针
    public ListNode middleNode1(ListNode head) {
        if(head==null){return null;}
        ListNode slow = head;
        ListNode fast = head;
        while (fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;

    }
}
